<?php 

class ModuleModel extends CI_Model {
    public $listeQuestionAll;
    public $listeQuestionModule;
    public $listeEnseignant;
    public $module;
    
    function getData()
    {        
        return array(            
            'listeQuestionAll'=>$this->listeQuestionAll,
            'listeQuestionModule'=>$this->listeQuestionModule,
            'listeEnseignant'=>$this->listeEnseignant,
            'module'=>$this->module
        );
    }
    
    function getModule()
    {
        $this->db->select('*');
        $this->db->from('module');
        $this->db->where('idmodule',$_POST["choixModule"]);
        $this->module = $this->db->get()->result();           
    }
    
    function getListeQuestions($_choixModule="")
    {
        $this->db->select('idquestion,Nomquestion,typequestion,ordre,Categquestion');
        $this->db->from('questions_view');  
        $this->db->join('module_has_questions',"questions_view.idquestion=module_has_questions.questions_idquestions");
        if ($_choixModule=="")
        {
            $this->db->where('module_idmodule',$_POST["choixModule"]);         
        }
        else
        {
            $this->db->where('module_idmodule',$_choixModule);          
        }
        $this->listeQuestionModule = $this->db->get();                
    }
    
      function getListeQuestionsAll()
    {             
        $this->getListeQuestions($_POST["choixModule"]);
        
        $this->db->select('idquestion,Nomquestion,typequestion,Categquestion');
        $this->db->from('questions_view');
//        $this->db->join('module_has_questions',"questions_view.idquestion=module_has_questions.questions_idquestions");
//        $this->db->where('module_idmodule <>',$_POST["choixModule"]); 
        foreach ($this->listeQuestionModule->result() as $row)
        {
            $this->db->where('idquestion <>',$row->idquestion); 
        }            
        $this->listeQuestionAll = $this->db->get();                
    }
    
    function getEnseignant()
    {        
        $this->db->select('iduser,user.nom,prenom,idmodule');
        $this->db->from('user');
        $this->db->join('module','module.idenseignant=user.iduser AND idmodule='.$_POST["choixModule"],'left');
        $this->db->where('typeuser',2);
        $this->db->order_by('user.nom,prenom');
        $this->listeEnseignant = $this->db->get();         
    }
    
    function addQuestions()
    {
        $dataQuestion = array(
            'Nomquestion' => $_POST["libelleQuestion"] ,
            'typequestion' => $_POST["TypeQuestion"] ,            
            'Categquestion' => $_POST["CategorieQuestion"]
        );         
        $this->db->insert('questions', $dataQuestion); 
        $last_idQuestion = $this->db->insert_id();
            
        $dataModulequestion = array(
            'module_idmodule' => $_POST["choixModule"] ,
            'questions_idquestions' => $last_idQuestion ,
            'ordre'=>    $_POST["ordreQuestion"] 
        );
        $this->db->insert('module_has_questions', $dataModulequestion);        
    }
    
    function supprimerQuestion()
    {        
        $this->db->delete("module_has_questions",array('module_idmodule'=>$_POST["idmodule"],'questions_idquestions'=>$_POST["idquestion"]));
        if ($this->db->_error_message()) {
            $result = 'Error! ['.$this->db->_error_message().']';            
            return false;
        } else {
            return true;
        }
    }  
    
    function ajoutChoixQuestion()
    {
        $dataModulequestion = array(
            'module_idmodule' => $_POST["idmodule"] ,
            'questions_idquestions' => $_POST["idquestion"] ,
            'ordre'=> $_POST["ordreQuestion"]
        ); 
        $this->db->insert('module_has_questions',$dataModulequestion);
        if ($this->db->_error_message()) {
            $result = 'Error! ['.$this->db->_error_message().']';            
            return false;
        } else {
            return true;
        }
    }
    
    function validerEnseignantModule()
    {
       
        $this->db->where('idmodule',$_POST["idmodule"]);
        $this->db->update('module',array('idenseignant' => $_POST["idenseignant"]));       
        
        if ($this->db->_error_message()) {
            $result = 'Error! ['.$this->db->_error_message().']';            
            return false;
        } else {
            return true;
        } 
    }
 
	
    function isModuleQuestionRealise()
    {
        $this->db->select('*');
        $this->db->from('resultateval');
        $this->db->where('iduser',$this->session->userdata('id'));
        $this->db->where('idmodule', $_POST["idModule"]);
        $query=$this->db->get();
        $rowcount = $query->num_rows();
        if($rowcount>0)
        {
            return true;
        }
        else
        {
            return false;
        }
       
    }
	
	
    function getListe()
    {
        $this->db->select('*');
        $this->db->from('module');
		
        return $this->db->get()->result();           
    }
    
    
    function getModuleUtilisateur($userid)
    {
        $this->db->select('*');
        $this->db->from('module');
        $this->db->join('module_has_user', 'module_has_user.module_idmodule = module.idmodule');
        $this->db->where('user_iduser', $userid);
        return $this->db->get()->result();
    }
    
    
    function getModuleFormation($id=null)
    {
        $this->db->select('*');
        $this->db->from('module');
        $this->db->join('formation_has_module', 'formation_has_module.module_idmodule = module.idmodule','left');
        
		if ($id !== null) {
                   
			$this->db->where('formation_idformation', $id);
		}
		
        return $this->db->get()->result();           
    }
	
}
